
#include <BLabel>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BGridLayout>

#include "CustomTagDialog.h"

CustomTagDialog::CustomTagDialog(BWidget* parent) : BDialog(parent)
{
	this->setTitle("Custom Tag");

	BGridLayout* topLayout = new BGridLayout();
	{
		_nameEdit = new BLineEdit();
		_classEdit = new BLineEdit();
		topLayout->addWidget(0, 0, new BLabel("name"));
		topLayout->addWidget(0, 1, _nameEdit);
		topLayout->addWidget(1, 0, new BLabel("class"));
		topLayout->addWidget(1, 1, _classEdit);
	}

	_listWidget = new BListWidget();

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(topLayout);
	vlayout->addWidget(_listWidget);

	BHBoxLayout* bottomLayout = new BHBoxLayout(this, Part_Bottom);
	{
		_cancelButton = new BButton("Cancel");
		_confirmButton = new BButton("Confirm");
		bottomLayout->addWidget(_cancelButton);
		bottomLayout->addSpring(2);
		bottomLayout->addWidget(_confirmButton);
	}

	connect(_nameEdit, Signal_EditFinished, &CustomTagDialog::slotNameEditFinished);
	connect(_listWidget, Signal_ItemClicked, &CustomTagDialog::slotListItemClicked);
	connect(_listWidget, Signal_ItemDoubleClicked, &CustomTagDialog::slotListItemDoubleClicked);

	connect(_cancelButton, Signal_Clicked, &CustomTagDialog::slotCancelButtonClicked);
	connect(_confirmButton, Signal_Clicked, &CustomTagDialog::slotConfirmButtonClicked);
}
CustomTagDialog::~CustomTagDialog()
{

}

const BString& CustomTagDialog::tagName() const
{
	return _nameEdit->text();
}
const BString& CustomTagDialog::tagClass() const
{
	return _classEdit->text();
}

void CustomTagDialog::slotNameEditFinished(BObject* sender, const BValue& value)
{
	_confirmButton->setFocused(true);
}
void CustomTagDialog::slotListItemClicked(BObject* sender, const BValue& value)
{
	if (BListItem* item = value)
	{
		_nameEdit->setText(item->text());
	}
}
void CustomTagDialog::slotListItemDoubleClicked(BObject* sender, const BValue& value)
{
	_confirmButton->click();
}

void CustomTagDialog::slotCancelButtonClicked()
{
	this->close();
}
void CustomTagDialog::slotConfirmButtonClicked()
{
	this->close(true);
}

void CustomTagDialog::popupEvent(const BEvent& event)
{
	if (event.value())
	{
		_confirmButton->setFocused(true);
		event.accept();
	}
	BDialog::popupEvent(event);
}
